package com.hanter.rad.widget.cupertino.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.hanter.pickerview.utils.R;
import com.hanter.rad.widget.cupertino.PickerView;

public class PickerDialog extends BaseDialog implements View.OnClickListener {

    public static final String TAG = "PickerDialog";

    private PickerView pvOptions;

    @Nullable
    protected PickerProvider provider;
    @Nullable
    protected OnPickerClickListener listener;

    public PickerDialog() {
        super();
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.PickerDialog);
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();

        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;
            window.setAttributes(lp);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.options_picker_view, container, false);
        initViews(contentView);
        return contentView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (context instanceof OnPickerClickListener) {
            listener = (OnPickerClickListener) context;
        }
    }

    private void initViews(View contentView) {
        TextView tvTitle = contentView.findViewById(R.id.tvTitle);
        Button btnSubmit = contentView.findViewById(R.id.btnSubmit);
        Button btnCancel = contentView.findViewById(R.id.btnCancel);
        btnSubmit.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        pvOptions = contentView.findViewById(R.id.pvOptions);
        if (provider != null) {
            pvOptions.setDataSource(provider.getDataSource(getTag()));
            pvOptions.setPickerDelegate(provider.getDelegate(getTag()));
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnCancel) {

            dismiss();

            if (listener != null) {
                listener.onPickerCancel(getTag());
            }
        } else if (v.getId() == R.id.btnSubmit) {
            dismiss();

            if (listener != null) {
                int[] indexes = new int[pvOptions.getNumberOfComponents()];
                for (int i = 0; i < indexes.length; i++) {
                    indexes[i] = pvOptions.getSelectedIndex(i);
                }
                listener.onPickerSelected(getTag(), indexes);
            }
        }
    }

    @Nullable
    public OnPickerClickListener getListener() {
        return listener;
    }

    public void setListener(@Nullable OnPickerClickListener listener) {
        this.listener = listener;
    }

    public static void dismiss(@Nullable DialogFragment dialog) {
        if (dialog != null && dialog.isDetached()) {
            dialog.dismissAllowingStateLoss();
        }
    }

    public boolean isDismissed() {
        Dialog dialog = getDialog();
        return dialog == null || !dialog.isShowing();
    }
}
